class counterItem {
    id: string = "";
    count: number = 0;
    used: number = 0;
}
class counter {
    day: number = 0;
    month: number = 0;
    Item: counterItem[] = [];
}

export default class counterControl {

    counterDate: counter[] = null;

    getCounter(counterType: number): counter {
        if (!this.counterDate)
            this.counterDate = gameHelper.getInstance().counterParam;
        if (!this.counterDate) {
            this.counterDate = [];
            this.counterDate.push(this.getCounterTime(0));
            this.counterDate.push(this.getCounterTime(1));
            this.counterDate.push(this.getCounterTime(2));
            let ever = new counter();
            this.counterDate.push(ever);
        }
        return this.checkCounter(counterType, this.counterDate[counterType]);
    }
    private checkCounter(counterType: number, element: counter) {
        if (this.checkTime(element)) {
            let time = this.getNewTime(counterType);
            element.month = time.getMonth();
            element.day = time.getDate();
            for (let index = 0; index < element.Item.length; index++) {
                let item = element.Item[index];
                item.used = 0;
            }
        }
        return element;
    }
    private checkTime(item: counter): boolean {
        let time = new Date();
        let month = time.getMonth();
        if (month == item.month) {
            if (time.getDate() >= item.day) {
                return true;
            }
        }
        else if (month > item.month) {
            return true;
        }
        else if (month == 1 && item.month == 12) {
            return true;
        }
        return false;
    }
    getCounterItem(counterType: number, id: string): counterItem {
        let date = this.getCounter(counterType);
        if (date) {
            for (let index = 0; index < date.Item.length; index++) {
                const element = date.Item[index];
                if (element.id == id) {
                    return element;
                }
            }
        }
        return null;
    }
    setCounterItem(counterType: number, item: counterItem) {
        let date = this.getCounterItem(counterType, item.id);
        if (date != null) {
            date.count = item.count;
            date.used = item.used;
        }
        else {
            let count = this.getCounter(counterType);
            count.Item.push(item);
        }
    }
    seveCounter() {
        gameHelper.getInstance().counterParam = this.counterDate;
    }
    getCounterTime(counterType: number): counter {
        let date = new counter();
        let time = this.getNewTime(counterType);
        date.month = time.getMonth();
        date.day = time.getDate();
        return date;
    }
    getNewTime(counterType: number): Date {
        let t = new Date();
        if (counterType == 0)
            return new Date(t.getFullYear(), t.getMonth(), t.getDate() + 1);
        else if (counterType == 1) {
            let week = t.getDay();
            let addTime = 7 - week;
            if (addTime == 0)
                addTime = 7;
            return new Date(t.getFullYear(), t.getMonth(), t.getDate() + addTime);
        }
        else
            return new Date(t.getFullYear(), t.getMonth() + 1, 1);
    }
}
